package net.rrkk.photoman.service;

import net.rrkk.photoman.entity.PhotoCategory;
import net.rrkk.photoman.repository.PhotoCategoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service
public class PhotoCategoryServiceImpl implements PhotoCategoryService {
    
    @Autowired
    private PhotoCategoryRepository categoryRepository;
    
    @Override
    public PhotoCategory createCategory(PhotoCategory category) {
        category.setCreatedAt(LocalDateTime.now());
        category.setUpdatedAt(LocalDateTime.now());
        // 如果没有设置level，根据parent_id设置level
        if (category.getLevel() == null) {
            if (category.getParentId() == null) {
                category.setLevel(0);
            } else {
                Optional<PhotoCategory> parentCategory = categoryRepository.findById(category.getParentId());
                if (parentCategory.isPresent()) {
                    category.setLevel(parentCategory.get().getLevel() + 1);
                } else {
                    category.setLevel(0);
                }
            }
        }
        return categoryRepository.save(category);
    }
    
    @Override
    public Optional<PhotoCategory> getCategoryById(Long id) {
        return categoryRepository.findById(id);
    }
    
    @Override
    public List<PhotoCategory> getAllCategories() {
        return categoryRepository.findAll();
    }
    
    @Override
    public List<PhotoCategory> getTopLevelCategories() {
        return categoryRepository.findByParentIdIsNull();
    }
    
    @Override
    public List<PhotoCategory> getActiveCategories() {
        return categoryRepository.findByIsActiveTrue();
    }
    
    @Override
    public List<PhotoCategory> getActiveTopLevelCategories() {
        return categoryRepository.findByParentIdIsNullAndIsActiveTrue();
    }
    
    @Override
    public List<PhotoCategory> getSubCategories(Long parentId) {
        return categoryRepository.findByParentId(parentId);
    }
    
    @Override
    public List<PhotoCategory> getActiveSubCategories(Long parentId) {
        return categoryRepository.findByParentIdAndIsActiveTrue(parentId);
    }
    
    @Override
    public List<PhotoCategory> searchCategoriesByName(String name) {
        return categoryRepository.findByNameContaining(name);
    }
    
    @Override
    public PhotoCategory updateCategory(Long id, PhotoCategory categoryDetails) {
        Optional<PhotoCategory> optionalCategory = categoryRepository.findById(id);
        if (optionalCategory.isPresent()) {
            PhotoCategory category = optionalCategory.get();
            category.setName(categoryDetails.getName());
            category.setParentId(categoryDetails.getParentId());
            category.setSortOrder(categoryDetails.getSortOrder());
            category.setIsActive(categoryDetails.getIsActive());
            category.setUpdatedAt(LocalDateTime.now());
            
            // 更新level
            if (category.getParentId() == null) {
                category.setLevel(0);
            } else {
                Optional<PhotoCategory> parentCategory = categoryRepository.findById(category.getParentId());
                if (parentCategory.isPresent()) {
                    category.setLevel(parentCategory.get().getLevel() + 1);
                } else {
                    category.setLevel(0);
                }
            }
            
            return categoryRepository.save(category);
        }
        return null;
    }
    
    @Override
    public void deleteCategory(Long id) {
        categoryRepository.deleteById(id);
    }
    
    @Override
    public List<Object[]> getCategoryTree() {
        return categoryRepository.findCategoryTree();
    }
}